<?php

/**

 * Created by PhpStorm.

 * User: Administrator

 * Date: 2019/11/8

 * Time: 8:26

 */

namespace app\api\controller;

use think\Db;

use think\Request;

use app\api\model\Food as FoodStore;

class FoodManage extends Nocommon

{

    /*

     * wode 修改美食管理头像

     */

    public function edit_food_icon()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store')->where('food_store_id',$post['food_store_id'])->setField('food_store_icon',$post['food_store_icon']);

            if($res){

                $this->apiReturn('200','修改成功');

            }else{

                $this->apiReturn('400','修改失败');

            }

        }

    }



    /*

     * 修改人均消费

     */

    public function food_people_money()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store')->where('food_store_id',$post['food_store_id'])->setField('food_people_money',$post['food_people_money']);

            if($res){

                $this->apiReturn('200','修改成功');

            }else{

                $this->apiReturn('400','修改失败');

            }

        }

    }



    /*

     * 修改营业时间

     */

    public function food_time()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store')->where('food_store_id',$post['food_store_id'])->setField('food_store_time',$post['food_store_time']);

            if($res){

                $this->apiReturn('200','修改成功');

            }else{

                $this->apiReturn('400','修改失败');

            }

        }

    }



    /*

     * 个性标签修改

     */

    public function edit_tag()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store')->where('food_store_id',$post['food_store_id'])->setField('food_tag',$post['food_tag']);

            if($res){

                $this->apiReturn('200','修改成功');

            }else{

                $this->apiReturn('400','修改失败');

            }

        }

    }



    /*

     * 编辑 店铺介绍

     */

    public function food_content()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store')->where('food_store_id',$post['food_store_id'])->setField('food_store_content',$post['food_store_content']);

            if($res){

                $this->apiReturn('200','修改成功');

            }else{

                $this->apiReturn('400','修改失败');

            }

        }

    }



    /*

     * 店铺展示 页面

     */

    public function show_food()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store')->field('food_store_cover')->where('food_store_id',$post['food_store_id'])->find();

            $res['food_store_cover'] = addWaterByField($res['food_store_cover']);

            $this->apiReturn('200','成功',$res);

        }

    }

    /*

     * 店铺图片 展示

     */

    public function store_img_show()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food_store_img')->field('id,img_path')->where('store_id',$post['food_store_id'])->select();

            if(count($res)>0){

                foreach ($res as $key => $value) {

                    $res[$key]['img_path'] = addWaterByField($value['img_path']);

                }

            }

            $this->apiReturn('200','成功',$res);

        }

    }

    /*

     * 店铺图片 上传

     */

    public function store_upload_img()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $data = explode(',',$post['img_path']);

            foreach($data as $k =>$v){

                $insert = [

                    'store_id'=>$post['food_store_id'],

                    'img_path'=>$v,

                    'create_time'=>date('Y-m-d H:i:s'),

                ];

                $res = Db::name('food_store_img')->insert($insert);

            }

            if($res){

                $this->apiReturn('200','添加成功');

            }else{

                $this->apiReturn('400','添加失败');

            }

        }

    }



    /*

     * 删除图片

     */

    public function store_delete_img()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            //查询所有ID

            $id = $post['id'];

            $data = Db::name('food_store_img')->where("id in ($id)")->delete();

            if($data){

                $this->apiReturn('200','删除成功');

            }else{

                $this->apiReturn('400','删除失败');

            }



        }

    }



    /*

     * 美食展示

     */

    public function food_show()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $res = Db::name('food')->field('food_id,store_id,food_title,food_cover')->where('store_id',$post['food_store_id'])->select();

             if(count($res)>0){

                foreach ($res as $key => $value) {

                    $res[$key]['food_cover'] = addWaterByField($value['food_cover']);

                }

            }

            $this->apiReturn('200','成功',$res);

        }

    }

    /*

     * 添加美食

     */

    public function food_insert()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();

            $data = [

                'store_id'=>$post['food_store_id'],

                'food_title'=>$post['food_title'],

                'food_cover'=>$post['food_cover'],

                'create_time'=>date('Y-m-d H:i:s')

            ];

            $res = Db::name('food')->insert($data);

            if($res){

                $this->apiReturn('200','添加成功');

            }else{

                $this->apiReturn('400','添加失败');

            }



        }

    }



    /*

     * 修改美食

     */

    public function food_update()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();
            if(!empty(substr($post['food_cover'],0,strrpos($post['food_cover'],"-")))){
                 $data = [

                    'food_title'=>$post['food_title'],

                    'food_cover'=>substr($post['food_cover'],0,strrpos($post['food_cover'],"-")),

                    'update_time'=>date('Y-m-d H:i:s')

                ];
            }else{
                 $data = [

                    'food_title'=>$post['food_title'],

                    'food_cover'=>$post['food_cover'],

                    'update_time'=>date('Y-m-d H:i:s')

                ];
            }
           

            $res = Db::name('food')->where('food_id',$post['food_id'])->update($data);

            if($res){

                $this->apiReturn('200','修改成功');

            }else{

                $this->apiReturn('400','修改失败');

            }

        }

    }

    /*

     * 删除美食

     */

    public function food_delete()

    {

        if(Request::instance()->isPost()){

            $post = Request::instance()->post();



            $res = Db::name('food')->where('food_id',$post['food_id'])->delete();

            if($res){

                $this->apiReturn('200','删除成功');

            }else{

                $this->apiReturn('400','删除失败');

            }

        }

    }



}